#include "sys_config.h"




/**
 * @brief  串口初始化
 * @note 115200(波特率) 8(数据位) 1(停止位) 0(校验) 0(流控)
 * @param {*}
 * @return {*}
 */
void uart_init(void)
{
    uint32_t ret = 0;
    // 初始化串口
    hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_UART2_TXD);
    hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_UART2_RXD);

    hi_uart_attribute uart_param = {
        .baud_rate = 115200,
        .data_bits = 8,
        .stop_bits = 1,
        .parity = 0,
    };
    ret = hi_uart_init(HI_UART_IDX_2, &uart_param, NULL);
    if (ret != HI_ERR_SUCCESS) {
        printf("hi uart init is faild.\r\n");
    }
}

/**
 * @brief  串口发送函数
 * @note 
 * @param str   缓存区首地址
 * @param len   数据长度
 * @return {*}
 */
void uart_send_buff(unsigned char *str, unsigned short len)
{
    hi_u32 ret = 0;
    ret = hi_uart_write(HI_UART_IDX_2, (uint8_t *)str, len);
    if (ret == HI_ERR_FAILURE) {
        printf("uart send buff is failed.\r\n");
    }
}

hi_u8 recvBuff[210] = {0};

/**
 * @brief  串口接收任务
 * @note   初始化串口，循环接收单片机的数据，进行Json解析，转发给上位机
 * @param {*}
 * @return {*}
 */
void uart_recv_task(void)
{
    hi_u8 *pbuff = recvBuff;

    hi_u8 uart_buff[20] = {0};
    
    int length = 0;
    #if 1
    while (1){
        // 阻塞接收
        if (memset_s((char *)uart_buff, sizeof(uart_buff), 0, sizeof(uart_buff)) == 0) {
            hi_u32 len = hi_uart_read(HI_UART_IDX_2, uart_buff, sizeof(uart_buff));
            //memset(uart_buff,0,sizeof(uart_buff));
            #if 1
            if (len > 1) {
                if (length + len < sizeof(recvBuff) - 1)
                {
                    memcpy_s((char *)pbuff, len, (char *)uart_buff, len);
                    pbuff += len;
                    length += len;
                }
                else
                {
                    uart_update_task();
                    

                    pbuff = recvBuff;
                    printf("recvBuff: %s\r\n", recvBuff);
                    memset(recvBuff, 0, sizeof(recvBuff));
                    length = 0;
                }
                
            }
            #endif
        }
    }
    #endif

#if 0
    while (1){
        //usleep(100000); //不能加循环，不然会导致重启
        // 阻塞接收
        if (memset_s((char *)uart_buff, sizeof(uart_buff), 0, sizeof(uart_buff)) == 0) {
            hi_u32 len = hi_uart_read(HI_UART_IDX_2, uart_buff, sizeof(uart_buff));
            if (len > 0) {


                if (pbuff > (recvBuff+200)) {
                    pbuff = recvBuff; // 回到recvBuff的头位置
                    printf("buff: %s, size: %d\r\n", recvBuff, strlen(recvBuff));
                    
                    /*Json解析串口数据*/
                    osMutexAcquire(globals_Mutex_ID, osWaitForever);
                    Json_Ana_RecvFromUart(recvBuff);
                    osMutexRelease(globals_Mutex_ID);

                /*封装Json数据发送给客户端*/
                    if(Flag_Ok_Tcp){

                        //Tcp_Send_Buff(recvBuff, sizeof(recvBuff));
                    }

                    memset_s((char *)recvBuff, sizeof(recvBuff), 0, sizeof(recvBuff));
                }
                memcpy_s((char *)pbuff, len, (char *)uart_buff, len);
                pbuff += len;            }
        }
    }
#endif
}

/**
 * @brief  串口更新任务
 * @note   根据接收单片机的数据，进行Json解析，转发给上位机
 * @param {*}
 * @return {*}
 */
void uart_update_task(void)
{
    char *temp, * temp1, *temp2;
    temp1 = strstr(recvBuff, "{\"status");
    temp2 = strstr(temp1, "}}");

    temp = strstr(temp1,"distance");

    
    printf("uart_recv_buff:%s\r\n", recvBuff);
    if (NULL != temp1 && NULL != temp2 && (15 >= (temp - temp1)))
    {
        printf("start ana\r\n");
        int ret = Json_Ana_RecvFromUart(temp1);
        if(ret)
            printf("ana error\r\n");
        else{
            printf("ana success\r\n");
            Udp_Send_To_Client();
        }
    }

}